﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using WDA.Core;
using Sample;
using System.Configuration;
using System.Threading;
using System.ServiceModel.Channels;

namespace GuiTest
{
   /// <summary>
   /// Interaction logic for Window1.xaml
   /// </summary>
   public partial class Window1 : Window
   {
      private Services _srv;
      private ResponseTimeValue _value = new ResponseTimeValue();

      public Window1()
      {
         InitializeComponent();
         TextBoxTraceListener.LogBox = logBox;
      }

      private void button3_Click(object sender, RoutedEventArgs e)
      {
         if (_srv == null)
         {
            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Containers["container"].Configure(container);
            container.RegisterInstance<IResponseTimeValue>(_value);
            
            UnityContainerElement servantContainerConfig = section.Containers["servantContainer"];
            UnityConfigModuleConfigurator servantConfigurator = new UnityConfigModuleConfigurator(WDAContainerNames.ServantContainerName, servantContainerConfig);
            container.RegisterInstance<IModuleConfigurator>(Guid.NewGuid().ToString(), servantConfigurator);

            PolicyInjectionSettings piabSettings =
               (PolicyInjectionSettings) ConfigurationManager.GetSection("policyInjection");
            PiabConfigModuleConfigurator piabServantConfigurator = new PiabConfigModuleConfigurator(WDAContainerNames.ServantContainerName, piabSettings);
            container.RegisterInstance<IModuleConfigurator>(Guid.NewGuid().ToString(), piabServantConfigurator);

            container.RegisterType<IServiceHostFactory, WcfServiceHostFactory>();
            container.RegisterInstance<Binding>(new BasicHttpBinding());

            _srv = new Services(container, configName.Text, hostName.Text, true);
            button3.Content = "Stop services";
         }
         else
         {
            _srv.Dispose();
            _srv = null;
            button3.Content = "Start services";
         }
      }

      private void button1_Click(object sender, RoutedEventArgs e)
      {
         //ThreadPool.QueueUserWorkItem(state => 				
         testResult.Text = _srv.Get<IFormatterService>().FormatDouble(
            _srv.Get<IConverterService>().ConvertToDouble(
            _srv.Get<IParserService>().ParseInt("1")));

      }

      private void button2_Click(object sender, RoutedEventArgs e)
      {
         //ThreadPool.QueueUserWorkItem(state =>
         _srv.Reconfigure(true);
      }

      private void button4_Click(object sender, RoutedEventArgs e)
      {
         _value.Milliseconds = int.Parse(reponseTimeValue.Text);
      }
   }
}
